package com.le.tester.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * @author Chadwick
 * 系统工具
 */
public class Tools {
    private static Logger log = LoggerFactory.getLogger(Tools.class);

    /**
     * 设置administrator后台账户
     */
    public static final String KEY_ADMIN_USER = "KEY_ADMIN_USER";
    public static final String KEY_MINI_USER = "KEY_MINI_USER";
    public static final String KEY_H5_USER = "KEY_H5_USER";
    public static final String KEY_ZB_PEOPLE = "KEY_ZB_PEOPLE";
    public static final String KEY_ZB_ASSISTANT = "KEY_ZB_ASSISTANT";

    public static final String KEY_YX_WORK_PC_USER = "KEY_YX_WORK_PC_USER";

    /**
     * 判断时间是否在时间段内
     *
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 验证手机号
     */
    public static boolean isPhone(String phone) {
        //匹配1开头并且是11位
        String regex = "^1\\d{10}$";
        return Pattern.matches(regex, phone);
    }

    /**
     * 验证邮箱
     */
    public static boolean isEmail(String email) {
        //匹配1开头并且是11位
        String regex = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        ;
        return Pattern.matches(regex, email);
    }

    /**
     * 号码隐藏处理
     */
    public static String hiddenPhoneCode(String phoneCode) {
        return phoneCode.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");

    }

    /**
     * 身份证号码隐藏处理
     */
    public static String hiddenIDCode(String IDCode) {
        if (IDCode.length() == 18) {
            return IDCode.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1*****$2");
        } else {
            return IDCode.replaceAll("(\\d{4})\\d{7}(\\w{4})", "$1*****$2");
        }
    }

    /**
     * 身份证号码是不是有效
     */
    public static boolean isIDNumber(String IDNumber) {
        // 定义判别用户身份证号的正则表达式（15位或者18位，最后一位可以为字母）
        String regularExpression = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
                "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";

        boolean matches = IDNumber.matches(regularExpression);
        //判断第18位校验值
        if (matches) {
            if (IDNumber.length() == 18) {
                try {
                    char[] charArray = IDNumber.toCharArray();
                    //前十七位加权因子
                    int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                    //这是除以11后，可能产生的11位余数对应的验证码
                    String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
                    int sum = 0;
                    for (int i = 0; i < idCardWi.length; i++) {
                        int current = Integer.parseInt(String.valueOf(charArray[i]));
                        int count = current * idCardWi[i];
                        sum += count;
                    }
                    char idCardLast = charArray[17];
                    int idCardMod = sum % 11;
                    return idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase());

                } catch (Exception e) {
                    log.info("检验身份证号码异常：{}", e.getMessage());
                    return false;
                }
            }
        }
        return matches;
    }


    //jdk8使用的UUID
    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 当前日期年月日时分秒 + N位随机数生成订单编号
     *
     * @param leng
     * @return
     */
    public static String getDateAddRandomNo(int leng) {
        SimpleDateFormat simpleDateFormat;
        simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String str = simpleDateFormat.format(date);
        Random random = new Random();
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < leng; i++) {
            result.append(random.nextInt(10));
        }
        return str + result;// 当前时间
    }

    /**
     * N位随机数生成订单编号
     *
     * @param leng
     * @return
     */
    public static String getRandomNoByLength(int leng) {
        Random random = new Random();
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < leng; i++) {
            result.append(random.nextInt(10));
        }
        return result.toString();// 当前时间
    }

    /**
     * 获取当前时间 yyyyMMddHHmmss
     *
     * @return String
     */
    public static String getCurrTime() {
        Date now = new Date();
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String s = outFormat.format(now);
        return s;
    }


}
